--- 模块功能：终端发送主题-科信自控
-- @author zfh
-- @module terminalSendTheme
-- @license MIT
-- @copyright zfh
-- @release 2023.06.12
module(..., package.seeall)

require "nvm"
require "modbusrtu"
require "getLbsLoc"
require "modbusrtu"

-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
local alarm = "0"
-- 系统参数初始化
local ready, mqttHostKeson, mqttPortKeson, mqttKeepAliveKeson,
      mqttLoginNameKeson, mqttLoginPwmKeson, mqttTransportKeson,
      mqttConnectionTimeout, mqttCleanSession, mQTTVersion, mqttQos, mqttRetain,
      sysId, pKey, devtype, sn, meId, sVer, hVer, connType, mac, power, mode,
      dc, ntpOrNot, factoryId, lAlarmOrNot, iccid, imei, earthquakeOpenOrnot,
      manAutoSwitch, DelayTime = false, nvm.get("MqttHostKeson"),
                                 tonumber(nvm.get("MqttPortKeson")),
                                 nvm.get("MqttKeepAliveKeson"),
                                 nvm.get("MqttLoginNameKeson"),
                                 nvm.get("MqttLoginPwmKeson"),
                                 nvm.get("MqttTransportKeson"),
                                 nvm.get("MqttConnectionTimeout"),
                                 nvm.get("MqttCleanSession"),
                                 nvm.get("MQTTVersion"), nvm.get("MqttQos"),
                                 nvm.get("MqttRetain"), nvm.get("SysId"),
                                 nvm.get("PKey"), nvm.get("Devtype"),
                                 nvm.get("Sn"), nvm.get("MeId"),
                                 nvm.get("SVer"), nvm.get("HVer"),
                                 nvm.get("ConnType"), nvm.get("Mac"),
                                 nvm.get("Power"), nvm.get("Mode"),
                                 nvm.get("Dc"), nvm.get("NtpOrNot"),
                                 nvm.get("FactoryId"),
                                 tostring(nvm.get("LAlarmOrNot")),
                                 nvm.get("ICCID"), nvm.get("IMEI"),
                                 tostring(nvm.get("EarthquakeOpenOrnot")),
                                 nvm.get("ManAutoSwitch"),
                                 tostring(nvm.get("DelayTime"))

-- 设备实时数据初始化
local H_H_LimitAlarm, H_LimitAlarm, L_L_LimitAlarm, L_LimitAlarm, measuredValue,
      address, baudRate, pressureUnit, decimalPoint, pressValue, rangeL, rangeH,
      filterValue, aDCRealValue, pressValueConvert, longitude, latitude,
      solenoidValveOnOff, solenoidValveOnState = nvm.get("H_H_LimitAlarm"),
                                                 nvm.get("H_LimitAlarm"),
                                                 nvm.get("L_L_LimitAlarm"),
                                                 nvm.get("L_LimitAlarm"),
                                                 nvm.get("MeasuredValue"),
                                                 nvm.get("Address"),
                                                 nvm.get("BaudRate"),
                                                 nvm.get("PressureUnit"),
                                                 nvm.get("DecimalPoint"),
                                                 nvm.get("PressValue"),
                                                 nvm.get("RangeL"),
                                                 nvm.get("RangeH"),
                                                 nvm.get("FilterValue"),
                                                 nvm.get("ADCRealValue"),
                                                 nvm.get("PressValueConvert"),
                                                 nvm.get("Longitude"),
                                                 nvm.get("Latitude"),
                                                 nvm.get("solenoidValveOnOff"),
                                                 nvm.get("solenoidValveOnState")

-- 上报设备信息组装
function equipmentInformationAssembly(sysTime)
    local devInfoSend, devInfoSendTopic, seq, rssi
    rssi = net.getRssi()
    iccid = sim.getIccid()
    imei = misc.getImei()
    seq = "info" .. sysTime
    log.info("sysId", sysId)
    devInfoSendTopic = "/edge/single/" .. pKey .. "/" .. sn .. "/info"
    devInfoSend = "{" .. "\"devtype\":" .. "\"" .. devtype .. "\"" ..
                      ",\"sysId\":" .. "\"" .. sysId .. "\"" .. "\",seq\":" ..
                      "\"" .. seq .. "\"" .. ",\"ack\":\"0\"" ..
                      ",\"parameter\":{" .. "\"sVer\":" .. "\"" .. sVer .. "\"" ..
                      ",\"rssi\":" .. "\"" .. rssi .. "\"" .. ",\"hVer\":" ..
                      "\"" .. hVer .. "\"" .. ",\"meId\":" .. "\"" .. meId ..
                      "\"" .. ",\"connType\":" .. "\"" .. connType .. "\"" ..
                      ",\"power\":" .. "\"" .. power .. "\"" .. ",\"dc\":" ..
                      "\"" .. dc .. "\"" .. ",\"longitude\":" .. "\"" ..
                      longitude .. "\"" .. ",\"latitude\":" .. "\"" .. latitude ..
                      "\"" .. ",\"ct\":" .. "\"" .. sysTime .. "\"" .. "\"" ..
                      ",\"iccid\":" .. "\"" .. iccid .. "\"" .. "\"" ..
                      ",\"imei\":" .. "\"" .. imei .. "\"" ..
                      ",\"installTime\":" .. "\"" ..
                      tostring(getLbsLoc.InstallTime) .. "\"" .. "}," ..
                      "\"ts\":" .. "\"" .. sysTime .. "\"" .. "}"
    return devInfoSendTopic, devInfoSend
end

-- 设备上报实时数据
function quipmentReal_timeDataAssembly(sysTime, alarmGet)
    local topic, dataInfo, seq, rssi
    if alarmGet then
        alarm = alarmGet
    else
        alarm = modbusrtu.Alarm
    end
    log.info("Latitude:", nvm.get("Latitude"))
    seq = "rtg" .. sysTime
    topic = "/edge/single/" .. pKey .. "/" .. sn .. "/rtg"
    rssi = net.getRssi()
    iccid = sim.getIccid()
    imei = misc.getImei()
    dataInfo =
        "{" .. "\"devtype\":" .. "\"" .. devtype .. "\"" .. ",\"sysId\":" ..
            "\"" .. sysId .. "\"" .. ",\"seq\":" .. "\"" .. seq .. "\"" ..
            ",\"ack\":\"0\"" .. ",\"parameter\":{" .. "\"devtype\":" .. "\"" ..
            devtype .. "\"" .. ",\"Rssi\":" .. "\"" .. rssi .. "\"" ..
            ",\"sysId\":" .. "\"" .. sysId .. "\"" .. ",\"sVer\":" .. "\"" ..
            sVer .. "\"" .. ",\"hVer\":" .. "\"" .. hVer .. "\"" .. ",\"meId\":" ..
            "\"" .. meId .. "\"" .. ",\"connType\":" .. "\"" .. connType .. "\"" ..
            ",\"power\":" .. "\"" .. power .. "\"" .. ",\"model\":" .. "\"" ..
            mode .. "\"" .. "}" .. ",\"payload\":{" .. "\"PressValue\":" .. "\"" ..
            modbusrtu.PressValueConvert .. "\"" .. ",\"alarm\":" .. "\"" ..
            alarm .. "\"" .. ",\"solenoidValveOnOff\":" .. "\"" ..
            solenoidValveOnOff .. "\"" .. ",\"solenoidValveOnState\":" .. "\"" ..
            solenoidValveOnState .. "\"" .. ",\"HHLimitAlarm\":" .. "\"" ..
            H_H_LimitAlarm .. "\"" .. ",\"HLimitAlarm\":" .. "\"" ..
            H_LimitAlarm .. "\"" .. ",\"LLLimitAlarm\":" .. "\"" ..
            L_L_LimitAlarm .. "\"" .. ",\"LLimitAlarm\":" .. "\"" ..
            L_LimitAlarm .. "\"" .. ",\"Dc\":" .. "\"" .. dc .. "\"" .. "}," ..
            "\"ts\":" .. "\"" .. sysTime .. "\"" .. "}"
    return topic, dataInfo
end

-- 设备上报实时数据-科信
function quipmentReal_timeDataAssembly_keson(sysTime, alarmGet)
    local topic, dataInfo, seq, rssi
    if alarmGet then
        alarm = alarmGet
    else
        alarm = modbusrtu.Alarm
    end
    seq = "rtg" .. sysTime
    topic = "edge/single/" .. pKey .. "/" .. sn .. "/rtg"
    rssi = net.getRssi()
    iccid = sim.getIccid()
    imei = misc.getImei()
    dataInfo =
        "{" .. "\"devtype\":" .. "\"" .. devtype .. "\"" .. ",\"sysId\":" ..
            "\"" .. sysId .. "\"" .. ",\"seq\":" .. "\"" .. seq .. "\"" ..
            ",\"ack\":\"0\"" .. ",\"parameter\":{" .. "\"devtype\":" .. "\"" ..
            devtype .. "\"" .. ",\"rssi\":" .. "\"" .. rssi .. "\"" ..
            ",\"sysId\":" .. "\"" .. sysId .. "\"" .. ",\"sVer\":" .. "\"" ..
            sVer .. "\"" .. ",\"hVer\":" .. "\"" .. hVer .. "\"" .. ",\"meId\":" ..
            "\"" .. meId .. "\"" .. ",\"connType\":" .. "\"" .. connType .. "\"" ..
            ",\"power\":" .. "\"" .. power .. "\"" .. ",\"model\":" .. "\"" ..
            mode .. "\"" .. ",\"dc\":" .. "\"" .. dc .. "\"" .. ",\"ntpOrNot\":" ..
            "\"" .. ntpOrNot .. "\"" .. ",\"longitude\":" .. "\"" .. longitude ..
            "\"" .. ",\"factoryId\":" .. "\"" .. factoryId .. "\"" ..
            ",\"lAlarmOrNot\":" .. "\"" .. lAlarmOrNot .. "\"" ..
            ",\"earthquakeOpenOrnot\":" .. "\"" .. earthquakeOpenOrnot .. "\"" ..
            ",\"latitude\":" .. "\"" .. latitude .. "\"" ..
            ",\"manAutoSwitch\":" .. "\"" .. manAutoSwitch .. "\"" ..
            ",\"DelayTime\":" .. "\"" .. DelayTime .. "\"" .. "}" ..
            ",\"payload\":{" .. "\"pressValue\":" .. "\"" ..
            modbusrtu.PressValueConvert .. "\"" .. ",\"alarm\":" .. "\"" ..
            alarm .. "\"" .. ",\"solenoidValveOnOff\":" .. "\"" ..
            solenoidValveOnOff .. "\"" .. ",\"solenoidValveOnState\":" .. "\"" ..
            solenoidValveOnState .. "\"" .. ",\"hHLimitAlarm\":" .. "\"" ..
            H_H_LimitAlarm .. "\"" .. ",\"hLimitAlarm\":" .. "\"" ..
            H_LimitAlarm .. "\"" .. ",\"lLLimitAlarm\":" .. "\"" ..
            L_L_LimitAlarm .. "\"" .. ",\"lLimitAlarm\":" .. "\"" ..
            L_LimitAlarm .. "\"" .. ",\"rangeL\":" .. "\"" .. rangeL .. "\"" ..
            ",\"rangeH\":" .. "\"" .. rangeH .. "\"" .. "}," .. "\"ts\":" ..
            "\"" .. sysTime .. "\"" .. "}"
    return topic, dataInfo
end

-- 设备端召读设备数据
function CallingDeviceData(sysTime, seq)
    local topic, dataInfo, code, rssi
    topic = "/edge/single/" .. pKey .. "/" .. sn .. "/call"
    rssi = net.getRssi()
    dataInfo =
        "{" .. "\"devtype\":" .. "\"" .. devtype .. "\"" .. ",\"Rssi\":" .. "\"" ..
            rssi .. "\"" .. ",\"sysId\":" .. "\"" .. sysId .. "\"" ..
            ",\"seq\":" .. "\"" .. seq .. "\"" .. ",\"parameter\":{" ..
            "\"sVer\":" .. "\"" .. sVer .. "\"" .. ",\"hVer\":" .. "\"" .. hVer ..
            "\"" .. ",\"meId\":" .. "\"" .. meId .. "\"" .. ",\"connType\":" ..
            "\"" .. connType .. "\"" .. ",\"power\":" .. "\"" .. power .. "\"" ..
            "\"" .. ",\"Longitude\":" .. "\"" .. longitude .. "\"" ..
            ",\"Latitude\":" .. "\"" .. latitude .. "\"" .. ",\"ct\":" .. "\"" ..
            sysTime .. "\"" .. "}," .. ",\"payload\":{" .. "\"Dc\":" .. "\"" ..
            dc .. "\"" .. ",\"solenoidValveOnOff \":" .. "\"" ..
            solenoidValveOnOff .. ",\"solenoidValveOnState \":" .. "\"" ..
            solenoidValveOnState .. "\"pressValueConvert\":" .. "\"" ..
            modbusrtu.PressValueConvert .. "\"" .. ",\"LLLimitAlarm\":" .. "\"" ..
            L_L_LimitAlarm .. "\"" .. ",\"LLimitAlarm\":" .. "\"" ..
            L_LimitAlarm .. "\"" .. ",\"HLimitAlarm\":" .. "\"" .. H_LimitAlarm ..
            "\"" .. ",\"HHLimitAlarm\":" .. "\"" .. H_H_LimitAlarm .. "\"" ..
            "}," .. "\"ts\":" .. "\"" .. sysTime .. "\"" .. ",\"code\":" ..
            "200" .. "}"
    return topic, dataInfo
end

-- 无线关联报警上报报文
function pressureCutoffDeviceReportMsg(alarm)
    factoryId = nvm.get("FactoryId")
    local topic, dataInfo, code, rssi
    topic = sn .. "/presssureAlarmSend/set"
    dataInfo = "{" .. "\"alarmLevel\":" .. "\"" .. alarm .. "\"" ..
                   ",\"factoryId\":" .. "\"" .. factoryId .. "\"" ..
                   ",\"truePressure\":" .. "\"" .. modbusrtu.PressValueConvert ..
                   "\"" .. "}"
    return topic, dataInfo
end

-- 地震预警关联报警上报报文
function earthquakeWarningReportMsg(countDowns, intensity2F433)
    factoryId = nvm.get("FactoryId")
    local topic, dataInfo, code, rssi
    topic = sn .. "/earthquakeWarningSend/set"
    dataInfo = "{" .. "\"countDowns\":" .. "\"" .. countDowns .. "\"" ..
                   ",\"factoryId\":" .. "\"" .. factoryId .. "\"" ..
                   ",\"intensity2F433\":" .. "\"" .. intensity2F433 .. "\"" ..
                   "}"
    return topic, dataInfo
end

-- 地震预警关联报警取消上报报文
function earthquakeWarningCancelReportMsg()
    factoryId = nvm.get("FactoryId")
    local topic, dataInfo
    topic = sn .. "/earthquakeWarningCancelSend/set"
    dataInfo = "{" .. "\"todo\":\"cancel\"" .. ",\"factoryId\":" .. "\"" ..
                   factoryId .. "\"" .. "}"
    return topic, dataInfo
end

-- 设备端参数配置修改 设备端修改参数不主动上传修改的参数
function changeDeviceData(sysTime, ...)
    result = 0
    local arg = {...} -- > arg 为一个表，局部变量
    for i, v in ipairs(arg) do result = result + v end
    print("总共传入 " .. #arg .. " 个数") -- > #arg = 6
    return result / #arg
end
